package com.xhwl.centiotdata.suppliersystem.sinoiov;

import cn.hutool.http.HttpUtil;
import com.xhwl.common.pojo.cent.sdata.personnel.StaffDTO;
import com.xhwl.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author pengjunming
 * @description
 * @date 2022/11/9 16:10
 */
@Service
@Slf4j
public class PedestrianSinoiovHandler {

    public String add(StaffDTO dto, String host, String agentId, String openId) {
        JSONObject param = new JSONObject();
        if (StringUtils.isNotEmpty(openId)) {
            param.put("id", openId);
        }
        param.put("realName", dto.getName());
        param.put("agentId", agentId);
        param.put("phone", dto.getPhone());
        param.put("userCode", String.valueOf(dto.getAccountId()));
        param.put("roleCode", "");
        try {
            String createUrl = "/api/thirdParty/createAgentStaffExt";
            String jsonResult = HttpUtil.post(host + createUrl, param.toString());
            JSONObject jsonObject = JSONObject.parseObject(jsonResult);
            if (Objects.equals(jsonObject.getString("returnCode"), "Y")) {
                JSONObject userInfo = jsonObject.getJSONObject("obj");
                return userInfo.getString("userId");
            }
        } catch (Exception e) {
            log.error("中交兴路账号数据同步异常", e);
        }
        return null;
    }

    public boolean delete(String host, String agentId, String phone, String openId) {
        JSONObject param = new JSONObject();
        param.put("id", openId);
        param.put("agentId", agentId);
        param.put("phone", phone);
        try {
            String deleteUrl = "/api/thirdParty/deleteAgentStaffExt";
            String jsonResult = HttpUtil.post(host + deleteUrl, param.toString());
            JSONObject jsonObject = JSONObject.parseObject(jsonResult);
            if (Objects.equals(jsonObject.getString("returnCode"), "Y")) {
                return true;
            } else {
                log.error("删除中交兴路账号数据失败:{}", jsonResult);
            }
        } catch (Exception e) {
            log.error("删除中交兴路账号数据异常", e);
        }
        return false;
    }
}
